package itpenguin;

import itpenguin.common.PMFactory;
import itpenguin.entities.Article;
import itpenguin.entities.GeneralProperty;

import java.io.IOException;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class PenguinoneServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		
		PersistenceManager pm = null;
		try {
			pm = PMFactory.getInstance().getPersistenceManager();
			GeneralProperty gp = pm.getObjectById(GeneralProperty.class, "greeting");
			pm.detachCopy(gp);
			req.setAttribute("greeting", gp.value);
			Query query = pm.newQuery("select from itpenguin.entities.Article order by published desc");  
			query.setRange(0, 5);
			List<Article> list = (List<Article>)query.execute();
			req.setAttribute("articleList", pm.detachCopyAll(list));
		} finally {
			pm.close();
		}
		RequestDispatcher rd = req.getRequestDispatcher("/WEB-INF/template/main.jsp");
		rd.forward(req, resp);
	}

	
}
